import { defineStore } from 'pinia'
import { UserInfo } from '@renderer/types/store'
import { LanguageEnum } from '@renderer/enums/appEnum'
import { router } from '@renderer/router'

interface UserState {
  language: LanguageEnum // 语言
  isLogin: boolean // 是否登录
  info: Partial<UserInfo> // 用户信息
  accessToken: string // 添加访问令牌
  refreshToken: string // 添加刷新令牌
}

export const useUserStore = defineStore('userStore', {
  state: (): UserState => ({
    language: LanguageEnum.ZH,
    isLogin: false,
    info: {},
    accessToken: '',
    refreshToken: ''
  }),
  getters: {},
  actions: {
    logOut() {
      setTimeout(() => {
        this.info = {}
        this.isLogin = false
        this.accessToken = ''
        this.refreshToken = ''
        sessionStorage.removeItem('accessToken')
        sessionStorage.removeItem('iframeRoutes')
        router.push('/login')
      }, 300)
    }
  }
})
